package com.longteng.diamond.domain;

import com.longteng.diamond.constant.BuildTestStatus;
import com.longteng.diamond.po.RequestResult;
import com.longteng.diamond.util.ReportLogFileUtil;
import lombok.Data;

import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.List;

/**
 * Created by Administrator
 * DATE : 2018/8/15
 * TIME : 23:22
 * PROJECT : java-lesson
 * PACKAGE : com.longteng.lesson
 *
 * @author <a href="mailto:taki@qq.com">Administrator</a>
 */
@Data
public class RunTestContext extends BaseEntity {
    private NumberFormat numberFormat = NumberFormat.getInstance();
    private Integer buildTestId;
    private BuildTest buildTest;
    private BuildTestInterface buildTestInterface;
    private BuildTestCase buildTestCase;
    private List<BuildTestCase> testCaseList;
    private RequestResult requestResult;
    private ReportLogFileUtil reportLogFileUtil;
    private long startTime;
    private long endTime;
    private long buildStartTime;
    /**
     * 初始化运行上下文
     *
     * @return
     */
    public void initBuildTest(){
        BuildTest buildTest = this.getBuildTest();
        buildTest.setCaseSize(0);
        buildTest.setPassCaseSize(0);
        buildTest.setFailCaseSize(0);
    }

    /**
     * 初始化接口状态
     *
     * @return
     */
    public void setInterfaceAllSize(int interfaceAllSize) {
        BuildTestInterface buildTestInterface = this.getBuildTestInterface();
        buildTestInterface.setCaseSize(buildTestInterface.getCaseSize()+interfaceAllSize);
        this.getBuildTest().setCaseSize(this.getBuildTest().getCaseSize()+interfaceAllSize);
    }
    /**
     * 设置失败用例数量
     *
     * @return
     */
    public void setInterfaceFailSize(int interfaceFailSize) {
        BuildTestInterface buildTestInterface = this.getBuildTestInterface();
        Integer failSize = interfaceFailSize+buildTestInterface.getFailCaseSize();
        buildTestInterface.setFailCaseSize(failSize);
        this.getBuildTest().setFailCaseSize(interfaceFailSize+this.getBuildTest().getFailCaseSize());
    }
    /**
     * 设置通过用例数量
     *
     * @return
     */
    public void setInterfacePassSize(int interfacePassSize) {
        BuildTestInterface buildTestInterface = this.getBuildTestInterface();
        Integer passSize = interfacePassSize+buildTestInterface.getPassCaseSize();
        buildTestInterface.setPassCaseSize(passSize);
        this.getBuildTest().setPassCaseSize(interfacePassSize+this.getBuildTest().getPassCaseSize());
    }
    /**
     * 初始化接口状态
     *
     * @return
     */
    public void initInterfaceStatusSize() {
        BuildTestInterface buildTestInterface = this.getBuildTestInterface();
        buildTestInterface.setCaseSize(0);
        buildTestInterface.setFailCaseSize(0);
        buildTestInterface.setPassCaseSize(0);
        this.startTime = System.currentTimeMillis();
    }

    /**
     * 计算接口和构建的通过用例数量、运行时长、通过率
     *
     * @return
     */
    public void interfaceCalculation() {
        BuildTestInterface buildTestInterface = this.getBuildTestInterface();
        /**计算通过率的**/
        String passRate = numberFormat.format((float) buildTestInterface.getPassCaseSize() / (float) buildTestInterface.getCaseSize() * 100) + "%";
        buildTestInterface.setPassRate(passRate);
        long runTime = (this.getEndTime() - this.getStartTime()) / 1000;
        buildTestInterface.setRunTime(runTime + "秒");
        if (buildTestInterface.getCaseSize().intValue() == buildTestInterface.getPassCaseSize()) {
            buildTestInterface.setStatus(BuildTestStatus.pass);
        } else if (buildTestInterface.getCaseSize().intValue() == buildTestInterface.getFailCaseSize()) {
            buildTestInterface.setStatus(BuildTestStatus.fail);
        } else {
            buildTestInterface.setStatus(BuildTestStatus.partPass);
        }
        /**计算这次所有执行结果的**/
        BuildTest buildTest = this.getBuildTest();
        String buildRate = numberFormat.format((float) buildTest.getPassCaseSize() / (float) buildTest.getCaseSize() * 100) + "%";
        buildTest.setPassRate(buildRate);
        if (buildTest.getCaseSize().intValue() == buildTest.getPassCaseSize()) {
            buildTest.setStatus(BuildTestStatus.pass);
        } else if (buildTest.getCaseSize().intValue() == buildTest.getFailCaseSize()) {
            buildTest.setStatus(BuildTestStatus.fail);
        } else {
            buildTest.setStatus(BuildTestStatus.partPass);
        }
        long buildRunTime = (System.currentTimeMillis() - this.getBuildStartTime()) / 1000;
        buildTest.setRunTime(buildRunTime + "秒");
    }

}
